package com.game.modular.game.playerAssets.service;

import com.game.comment.exection.ServiceException;
import com.game.modular.game.playerAssets.domain.PlayerHero;
import com.game.modular.game.playerAssets.domain.PlayerSkill;
import com.game.modular.game.playerAssets.mapper.PlayerSkillMapper;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffectServiceImpl;
import com.game.modular.game.playerAssets.service.base.PlayerSkillBaseServiceImpl;
import com.game.modular.gameCore.battle.service.impl.CoreSkillEffectServiceImpl;
import com.game.modular.gameCore.cultivate.domain.CoreAccountConfig;
import com.game.modular.gameCore.cultivate.domain.CoreSkill;
import com.game.modular.gameCore.cultivate.domain.CoreSkillStar;
import com.game.modular.gameCore.cultivate.service.impl.CoreAccountConfigServiceImpl;
import com.game.modular.gameCore.cultivate.service.impl.CoreSkillServiceImpl;
import com.game.modular.gameCore.cultivate.service.impl.CoreSkillStarServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 游戏账户技能数据(PlayerSkill)表服务实现类
 *
 * @author cxy
 * @since 2024-07-18 10:48:34
 */
@Slf4j
@Service
public class PlayerSkillServiceImpl extends PlayerSkillBaseServiceImpl {
    @Resource
    private PlayerSkillMapper accountSkillMapper;
    @Resource
    private CoreAccountConfigServiceImpl coreAccountConfigServiceImpl;
    @Resource
    private CoreSkillStarServiceImpl coreSkillStarServiceImpl;
    @Resource
    private PlayerBagServiceImpl accountBagServiceImpl;
    @Resource
    private CoreSkillServiceImpl coreSkillServiceImpl;
    @Resource
    private CoreSkillEffectServiceImpl coreSkillEffectServiceImpl;
    @Resource
    private PvpSkillEffectServiceImpl PvpSkillEffectServiceImpl;

    /**
     * 占用技能
     */
    public void occupySkill(
            Long playerId,
            Long coreSkillStarId
    ) {
        PlayerSkill accountSkill = selectByCoreSkillStarId(playerId,coreSkillStarId);
        if (accountSkill == null){
            throw new ServiceException("未拥有当前技能");
        }
        if (accountSkill.getStatus() == 2) {
            throw new ServiceException("技能占用中");
        }
        accountSkill.setStatus(2);
        if (!updateById(accountSkill)) {
            throw new ServiceException("当前技能已穿戴");
        }
    }
    public void unOccupySkill(Long playerId, Long coreSkillStarId) {
        PlayerSkill accountSkill = selectByCoreSkillStarId(playerId,coreSkillStarId);
        assert accountSkill != null;
        if (accountSkill.getStatus() == 1) {
            throw new ServiceException("技能正常");
        }
        accountSkill.setStatus(1);
        if (accountSkillMapper.updateById(accountSkill) <= 0) {
            throw new ServiceException("技能无法解放占用");
        }
    }

    @Transactional
    public void registerInit(Long playerId) {
        CoreAccountConfig coreAccountConfig = coreAccountConfigServiceImpl.getCache(1L);
        // 物品信息
        String prop = coreAccountConfig.getSkills();
        String[] props = prop.split(",");
        List<String> giftList = Arrays.asList(props);
        giftList.forEach(skillStr -> {
            if (skillStr.contains("&")) {
                String[] split = skillStr.split("&");
                Long coreSkillId = Long.valueOf(split[0]);
                Integer value = Integer.valueOf(split[1]);
                obtainSkill(playerId, coreSkillId, value);
            }
        });
    }

    @Transactional
    public PlayerSkill obtainSkill(Long playerId, Long coreSkillId, Integer quantity) {
        PlayerSkill accountSkill = selectByCoreSkillId(playerId, coreSkillId);
        CoreSkill coreSkill = coreSkillServiceImpl.getCache(coreSkillId);
        CoreSkillStar coreSkillStar = coreSkillStarServiceImpl.getCache(coreSkillId, 0);
        if (accountSkill == null) {
            accountSkill = PlayerSkill.builder()
                    .uid(playerId)
                    .coreSkillId(coreSkillId)
                    .coreSkillStarId(coreSkillStar.getId())
                    .lv(coreSkill.getQualityLv())
                    .status(1)
                    .build();
            save(accountSkill);
            quantity = quantity - 1;
            if (quantity > 0) {
                skillToProp(playerId, coreSkillId, quantity);
            }
        } else {
            skillToProp(playerId, coreSkillId, quantity);
            log.info("玩家{}技能{}已转化为信物", playerId, accountSkill.getCoreSkillId());
        }
        return accountSkill;
    }

    private void skillToProp(Long playerId, Long coreSkillId, Integer quantity) {
        String prop = coreSkillStarServiceImpl.getProp(coreSkillId);
        if (prop == null || !prop.contains("&")){
            return;
        }
        Long propId = Long.valueOf(prop.split("&")[0]);
        // 添加物品
        accountBagServiceImpl.obtainBackpackItems(playerId, Long.valueOf(quantity), propId);
    }


    public List<CoreSkillStar> list(PlayerHero playerHero) {
        return playerHero.getSkillStarIds().stream().map(coreSkillStarId ->
            coreSkillStarServiceImpl.getCache(coreSkillStarId)
        ).collect(Collectors.toList());
    }
}
